import JSEncrypt from 'jsencrypt'
import { cmsConfig } from '@/core/index'

export default url => {
    const crud = window.$crud(url)

    const getUsers = (ids) => window.$axios({
        url: `${url}s`,
        method: 'post',
        data:{ids}
    })

    const updateState = (id,state) => window.$axios({
        url: `${url}/state/${id}`,
        method: 'put',
        params:{
            state
        }
    })

    const resetPassword = (id) => window.$axios({
        url: `${url}/resetpsw/${id}`,
        method: 'put'
    })

    const validatorPassword = (psw) => window.$axios({
        url: `${url}/validator/${psw}`,
        method: 'get'
    })
    
    const updatePassword = (req) => window.$axios({
        url: `${url}/password`,
        method: 'put',
        data:req
    })

    const login = async (account, password,params) => {
        var publicKeyRes = await window.$axios({
            url: `login/publicKey`,
            method: 'get',
            meta: {
                isToken: false
            }
        });

        let encrypt = new JSEncrypt()
        encrypt.setPublicKey(publicKeyRes.data.data)
        password = encrypt.encrypt(password)

        return window.$axios({
            url: `login`,
            method: 'post',
            meta: {
                isToken: false
            },
            params:params,
            data: {
                account,
                password
            }
        })
    }

    const loginCMS = async (account, password,params) => {
        var publicKeyUrl = cmsConfig.authorize ? `${process.env.VUE_APP_BASE_GEO}api/appadmin/auth/publickey` : `${process.env.VUE_APP_BASE_GEO}api/geocms/authorize/publickey`
        var loginUrl = cmsConfig.authorize ? `${process.env.VUE_APP_BASE_GEO}api/appadmin/auth/login` : `${process.env.VUE_APP_BASE_GEO}api/geocms/authorize/login`
        var headers = cmsConfig.authorize ? { 'App-Sign': cmsConfig.appSign } : {}

        var publicKeyRes = await window.$axios({
            url: publicKeyUrl,
            method: 'get',
            headers,
            meta: {
                isToken: false
            }
        });

        let encrypt = new JSEncrypt()
        encrypt.setPublicKey(`-----BEGIN PUBLIC KEY-----\n${publicKeyRes.data.data}\n-----END PUBLIC KEY-----`)
        password = encrypt.encrypt(password)

        var jwt = await window.$axios({
            url: loginUrl,
            method: 'post',
            meta: {
                isToken: false
            },
            headers,
            params:params,
            data: {
                userName: account,
                password: password,
                accountType: 0
            }
        })
        return new Promise((resolve) => {
            window.localStorage.setItem('nm-token', JSON.stringify(jwt.data.data))
            jwt.data.data['token'] = jwt.data.data.accessToken
            resolve(jwt)
        })
    }

    const loginByIdentityCode = async (code) => {
        return window.$axios({
            url: `login/code`,
            method: 'post',
            meta: {
                isToken: false
            },
            params: { code }
        })
    }
    
    const getLoginInfo = () => window.$axios({
        url: 'login/pc',
        method: 'get'
    })

    const removeCache = () => window.$axios({
        url: 'login/cache',
        method: 'post'
    })
    const getRootRids = () => window.$axios({
        url: `other/role/getrootrids`,
        method: 'get'
    })
    const getNextRids = (roleId) => window.$axios({
        url: `other/role/getnextrids/${roleId}`,
        method: 'get'
    })

    crud.syncData = (startTime) => window.$axios({
        url: `other/organizes/syncusers`,
        method: 'post',
        params:{startTime:startTime},
    })

    const getusersbyroleid = (roleId) => window.$axios({
        url: `other/role/getusersbyroleid/${roleId}`,
        method: 'get'
    })
    const GetRule = () => window.$axios({
        url: `systemconfig/otherPasswordRule/GetRule`,
        method: "get",
    });
    const GetRuleByRole = () => window.$axios({
        url: `systemconfig/otherPasswordRule/byrole`,
        method: "get",
    });

    const checkIndate = () => window.$axios({
        url: `systemconfig/otherPasswordRule/checkPasswordIndate`,
        method: "get",
    });

    const checkIndateByRole = () => window.$axios({ 
        url: `systemconfig/otherPasswordRule/checkpsdindatebyrole`,
        method: "get",
    });


    return {
        ...crud,
        getUsers,
        updateState,
        resetPassword,
        validatorPassword,
        updatePassword,

        login,
        loginCMS,
        getLoginInfo,

        removeCache,
        loginByIdentityCode,
        getRootRids,
        getNextRids,
        getusersbyroleid,
        GetRule,
        GetRuleByRole,
        checkIndate,
        checkIndateByRole

    }
}